package cn.windy.samary.module.admin.controller;

import cn.windy.samary.core.annotation.OpLog;
import cn.windy.samary.module.abstracts.controller.AbstractController;
import cn.windy.samary.module.admin.entity.Notify;
import cn.windy.samary.module.admin.service.INotifyRecordService;
import cn.windy.samary.module.admin.service.INotifyService;
import cn.windy.samary.utils.PageContent;
import cn.windy.samary.utils.Res;
import cn.windy.samary.utils.ShiroUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.session.mgt.eis.SessionDAO;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 通知公告
 *
 * @author zyl
 * @date 2018-06-05 16:00
 */
@Controller
@RequestMapping("/admin/notify")
public class NotifyController extends AbstractController {

    @Resource
    private SessionDAO sessionDAO;
    @Resource
    private INotifyService notifyService;
    @Resource
    private INotifyRecordService notifyRecordService;

    @GetMapping()
    @OpLog("打开通知公告主页")
    @RequiresPermissions("admin:notify:notify")
    public String index() {
        return "admin/notify/notify";
    }

    @ResponseBody
    @OpLog("查询通知公告列表")
    @GetMapping("/list")
    @RequiresPermissions("admin:notify:notify")
    public Res list(PageContent pageContent) {

        Example example = new Example(Notify.class);
        Example.Criteria criteria = example.createCriteria();
        Page<Object> page = PageHelper.offsetPage(pageContent.getOffset(), pageContent.getLimit());
        List<Notify> notifies = notifyService.selectByExample(example);
        return Res.rows(notifies, page.getTotal());
    }

    @GetMapping("/add")
    @RequiresPermissions("admin:notify:add")
    public String add() {
        return "admin/notify/notifyAdd";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("admin:notify:edit")
    public String edit(@PathVariable("id") String id, Model model) {
        Notify notify = notifyService.selectByPrimaryKey(id);
        model.addAttribute("notify", notify);
        return "admin/notify/notifyEdit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("admin:notify:add")
    public Res save(@Valid Notify notify) {
        int i = notifyService.insertSelective(notify);
        return i > 0 ? Res.ok() : Res.error();
    }

    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("admin:notify:edit")
    public Res update(@Valid Notify notify) {
        int i = notifyService.updateByPrimaryKeySelective(notify);
        return i > 0 ? Res.ok() : Res.error();
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ResponseBody
    @RequiresPermissions("admin:notify:remove")
    public Res remove(String id) {
        int i = notifyService.deleteByPrimaryKey(id);
        return i > 0 ? Res.ok() : Res.error();
    }

    /**
     * 删除
     */
    @PostMapping("/batchRemove")
    @ResponseBody
    @RequiresPermissions("admin:notify:batchRemove")
    public Res remove(@RequestParam("ids[]") String[] ids) {
        int i = notifyService.deleteByPrimaryKeys(Arrays.asList(ids));
        return i > 0 ? Res.ok() : Res.error();
    }

    @ResponseBody
    @GetMapping("/message")
    public Res message() {
        Example example = new Example(Notify.class);
        Example.OrderBy orderBy = example.orderBy("createTime").desc();
        Page<Object> page = PageHelper.offsetPage(0, 3);
        List<Notify> notifies = notifyService.selectByExample(example);

//        return Res.rows(notifies, page.getTotal());
        return Res.rows(new ArrayList(), 0L);
    }

    @GetMapping("/myNotify")
    public String myNotify() {
        return "admin/notify/myNotify";
    }

    @OpLog("查询我的通知列表")
    @ResponseBody
    @GetMapping("/myNotifyList")
    public Res myNotifyList(PageContent pageContent) {

        String userId = ShiroUtils.getUserId();
        Page<Object> page = PageHelper.offsetPage(pageContent.getOffset(), pageContent.getLimit());
        List<Map<String, Object>> notifies = notifyService.myNotifyList(userId);
        return Res.rows(notifies, page.getTotal());
    }

    @GetMapping("/read/{id}")
    @RequiresPermissions("oa:notify:edit")
    public String read(@PathVariable("id") String id, Model model) {
        Notify notify = notifyService.selectByPrimaryKey(id);
        //更改阅读状态

        model.addAttribute("notify", notify);
        return "admin/notify/myRead";
    }
}
